use rand::seq::SliceRandom;
use anyhow::Result;

const UPPER: &[u8] = b"ABCDEFGHJKLMNPQRSTUVWXYZ";

const LOWER: &[u8] = b"abcdefghijkmnpqrstuvwxyz";

const NUMBERS: &[u8] = b"123456789";

const SYMBOL: &[u8] = b"!@#$%^&*_";


pub fn process_genpass(uppercase: bool, lowercase: bool, numbers: bool, symbol: bool, len: u8)
                       -> anyhow::Result<String> {
    let mut rng = rand::thread_rng();
    let mut password = Vec::new();
    let mut chars = Vec::new();

    if uppercase {
        chars.extend_from_slice(UPPER);
        password.push(*UPPER.choose(&mut rng).expect("data empty"));
    };
    if lowercase {
        chars.extend_from_slice(LOWER);
        password.push(*LOWER.choose(&mut rng).expect("data empty"));
    };
    if numbers {
        chars.extend_from_slice(NUMBERS);
        password.push(*NUMBERS.choose(&mut rng).expect("data empty"));
    };
    if symbol {
        chars.extend_from_slice(SYMBOL);
        password.push(*SYMBOL.choose(&mut rng).expect("data empty"));
    };
    for _ in 0..(len - password.len() as u8) {
        // let idx  = rng.gen_range(0..chars.len());
        // password.push(chars[idx] as char);
        let c = chars.choose(&mut rng)
            .expect("chars won't be empty in this context");
        password.push(*c);
    };
    password.shuffle(&mut rng);

    let content = String::from_utf8(password)?;
    let strength = zxcvbn::zxcvbn(&content, &[])?;
    println!("password-> {},{:?}", content, strength);
    Ok(content)
}